package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.domain.StrategyTag;
import cn.wolfcode.luowowo.article.query.StrategyDetailQuery;
import cn.wolfcode.luowowo.article.service.*;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisRedisService;
import cn.wolfcode.luowowo.cache.util.RedisKeys;
import cn.wolfcode.luowowo.cache.vo.StrategyStatisVO;
import cn.wolfcode.luowowo.comment.domain.StrategyComment;
import cn.wolfcode.luowowo.comment.query.StrategyCommentQuery;
import cn.wolfcode.luowowo.comment.service.IStrategyCommentService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.serach.query.SearchQueryObject;
import cn.wolfcode.luowowo.serach.service.IStrategySearchService;
import cn.wolfcode.luowowo.serach.vo.StatisVO;
import cn.wolfcode.luowowo.website.annotation.RequireLogin;
import cn.wolfcode.luowowo.website.annotation.UserParam;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by admin on 2019/8/11.
 */
@Controller
@RequestMapping("/strategy")
public class StrategyController {

    @Reference
    private IStrategyService strategyService;

    @Reference
    private IStrategyDetailService strategyDetailService;

    @Reference
    private IDestinationService destinationService;

    @Reference
    private IStrategyCatalogService strategyCatalogService;

    @Reference
    private IStrategyTagService strategyTagService;

    @Reference
    private IStrategyCommentService commentService;

    @Reference
    private IStrategyStatisRedisService strategyStatisRedisService;

    @Reference
    private IStrategyCommendService strategyCommendService;

    @Reference
    private IStrategySearchService strategySearchService;

    @RequestMapping("")
    public String index(Model model) {
        model.addAttribute("commends", strategyCommendService.selectCommendTop5());
        // 存所有国内/外所有的vo
        strategyStatisRedisService.setStrategyStatisTops(RedisKeys.STRATEGY_STATIS_VO.getPrefix());
        //获取所有攻略排行
        List<StrategyStatisVO> strategyTops = strategyStatisRedisService.getStrategyTops();
        List<StrategyStatisVO> unabroadCds = new ArrayList<>();
        List<StrategyStatisVO> abroadCds = new ArrayList<>();
        for (StrategyStatisVO vo : strategyTops) {
            if (abroadCds.size() == 10 && unabroadCds.size() == 10) {
                break;
            }
            if (vo.isIsabroad()) {
                if (abroadCds.size() < 10) {
                    abroadCds.add(vo);
                }
            } else {
                if (unabroadCds.size() < 10) {
                    unabroadCds.add(vo);
                }
            }
        }

        // 存所有国内/外所有的vo
        strategyStatisRedisService.setStrategyStatisHotCds(RedisKeys.STRATEGY_STATIS_VO.getPrefix());
        List<StrategyStatisVO> hotCds = strategyStatisRedisService.getStrategyhotCds();
        model.addAttribute("unabroadCds", unabroadCds);
        model.addAttribute("abroadCds", abroadCds);
        model.addAttribute("hotCds", hotCds);


        // 主题推荐排行
        List<Map<String, Object>> data = strategySearchService.getThemeCommend();
        model.addAttribute("themeCds", data);


        List<StatisVO> chinas = strategySearchService.getConditionGroup(SearchQueryObject.CONDITION_TYPE_UN_ABROAD);
        model.addAttribute("chinas", chinas);

        List<StatisVO> abroads = strategySearchService.getConditionGroup(SearchQueryObject.CONDITION_TYPE_ABROAD);
        model.addAttribute("abroads", abroads);

        List<StatisVO> themes = strategySearchService.getConditionGroup(SearchQueryObject.CONDITION_TYPE_THEME);
        model.addAttribute("themes", themes);

        return "strategy/index";
    }


    @RequestMapping("searchPage")
    public String searchPage(Model model, @ModelAttribute("qo") SearchQueryObject qo) {
        if (qo.getType() == null || qo.getTypeValue() == null) {
            qo.setType(-1);
            qo.setTypeValue(-1);
        }
        model.addAttribute("page", strategySearchService.query(qo));
        return "strategy/searchPageTpl";
    }


    @RequestMapping("list")
    public String list(Model model, @ModelAttribute("qo") StrategyDetailQuery qo) {
        //toasts
        List<Destination> list = destinationService.queryForParent(qo.getDestId());
        Destination dest = list.remove(list.size() - 1);
        model.addAttribute("toasts", list);
        //dest
        model.addAttribute("dest", dest);
        // tags
        List<StrategyTag> tags = strategyTagService.list();
        model.addAttribute("tags", tags);

        // pageInfo
        PageInfo pageInfo = strategyDetailService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "strategy/list";

    }


    @RequestMapping("detail")
    public String getStrategyService(Model model, Long id, @UserParam UserInfo userInfo) {
        StrategyDetail detail = strategyDetailService.get(id);
        detail.setStrategyContent(strategyDetailService.getContent(detail.getId()));
        model.addAttribute("detail", detail);


        model.addAttribute("userInfo", userInfo);

        // 攻略阅读量
        strategyStatisRedisService.viewNumIncrease(id, 1);
        StrategyStatisVO vo = strategyStatisRedisService.getStrategyStatisVo(id);
        model.addAttribute("vo", vo);

        // 收藏集合
        List<Long> c = strategyStatisRedisService.getStrategyStatisFavor(id);
        model.addAttribute("c", c);

        return "/strategy/detail";
    }


    /**
     * 攻略评论保存或修改
     *
     * @param comment
     * @param qo
     * @param userInfo
     * @return
     */
    @RequestMapping("/commentAdd")
    @ResponseBody
    public Object commentAdd(StrategyComment comment, @ModelAttribute("qo") StrategyCommentQuery qo, @UserParam UserInfo userInfo) {

        comment.setUserId(userInfo.getId());
        comment.setUsername(userInfo.getNickname());
        comment.setHeadUrl(userInfo.getHeadImgUrl());
        comment.setCity(userInfo.getCity());
        comment.setLevel(userInfo.getLevel());
        commentService.saveOrUpdate(comment);

        // 评论数
        strategyStatisRedisService.replyNumIncrease(comment.getDetailId(), 1);
        StrategyStatisVO vo = strategyStatisRedisService.getStrategyStatisVo(comment.getDetailId());

       /* StrategyDetail detail = strategyDetailService.get(qo.getDetailId());
        detail.setReplynum(detail.getReplynum() + 1);
        strategyDetailService.updataReplyNum(detail.getId(), detail.getReplynum());*/
        return AjaxResult.SUCCESS.getData(vo.getReplynum());
    }


    @RequestMapping("/comment")
    public String comment(Model model, @ModelAttribute("qo") StrategyCommentQuery qo) {

        Page query = commentService.query(qo);
        model.addAttribute("page", commentService.query(qo));

        return "strategy/commentTpl";
    }

    @RequestMapping("commentThumbUp")
    @ResponseBody
    public Object commentThumbUp(String toid, Long fromid) {

        commentService.commentThumbUp(toid, fromid);

        return AjaxResult.SUCCESS;
    }


    @RequestMapping("favor")
    @ResponseBody
    public Object favor(Long sid, @UserParam UserInfo userInfo) {

        if (userInfo == null) {
            AjaxResult result = new AjaxResult(false, "请先登录");
            result.setCode(102);
            return result;
        }
        boolean ret = strategyStatisRedisService.favor(sid, userInfo.getId());

        StrategyStatisVO vo = strategyStatisRedisService.getStrategyStatisVo(sid);

        return new AjaxResult(ret).getData(vo);
    }

    @RequestMapping("strategyThumbup")
    @ResponseBody
    public Object strategyThumbup(Long sid, @UserParam UserInfo userInfo) {
        if (userInfo == null) {
            AjaxResult result = new AjaxResult(false, "请先登录");
            result.setCode(102);
            return result;
        }
        boolean ret = strategyStatisRedisService.strategyThumbup(sid, userInfo.getId());

        StrategyStatisVO vo = strategyStatisRedisService.getStrategyStatisVo(sid);

        return new AjaxResult(ret).getData(vo);
    }

    /**
     * 分享加分
     *
     * @param sid
     * @param model
     * @return
     */
    @RequestMapping("/share")
    @ResponseBody
    @RequireLogin
    public Object share(Long sid, Model model) {
        strategyStatisRedisService.SharenumIncrease(sid, 1);
        return AjaxResult.SUCCESS;
    }
}